//
//  Register.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "Register.h"
#import "LXC_DataBaseHandle.h"
#import "UserModel.h"

@interface Register () <UITextFieldDelegate>

@end

@implementation Register

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"注册";
    
    NSArray *arr1 = @[@"用户名:", @"密码:", @"确认密码:", @"邮箱:", @"联系方式:"];

    for (int i = 0; i < 5; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50 + 70 * i, 100, 30)];
        label.backgroundColor = [UIColor clearColor];
        label.text = arr1[i];
        [self.view addSubview:label];
        
        UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(160, 50 + 70 * i, 180, 30)];
        tf.backgroundColor = [UIColor clearColor];
        tf.placeholder = [NSString stringWithFormat:@"请输入%@", arr1[i]];
        tf.borderStyle = UITextBorderStyleRoundedRect;
        tf.tag = 4000 + i;
        tf.delegate = self;
        tf.clearButtonMode = UITextFieldViewModeAlways;
        if (i == 1 || i == 2) {
            tf.secureTextEntry = YES;
        }
        [self.view addSubview:tf];
        [tf release];
        
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStylePlain) target:self action:@selector(regAction:)];

}
- (void)regAction:(UIBarButtonItem *)button {
    
    UITextField *tf1 = [self.view viewWithTag:4000];
    UITextField *tf2 = [self.view viewWithTag:4001];
    UITextField *tf3 = [self.view viewWithTag:4002];
    UITextField *tf4 = [self.view viewWithTag:4003];
    UITextField *tf5 = [self.view viewWithTag:4004];
    
    
    if (tf1.text.length > 0 && tf2.text.length > 0 && tf3.text.length > 0 && tf4.text.length > 0 && tf5.text.length > 0) {
        if ([tf2.text isEqualToString:tf3.text]) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                
                UserModel *userModel = [UserModel userModel];
                userModel.userName = tf1.text;
                userModel.password = tf2.text;
                userModel.email = tf3.text;
                userModel.telephone = tf5.text;
                
                [[LXC_DataBaseHandle shareDataBaseHandle] insertModelToUserTab:userModel];
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
            
            
        }else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"密码和确认密码不一致" preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        
    }else {
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"存在未填写的选项" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    UITextField *tf = [self.view viewWithTag:textField.tag + 1];
    [textField resignFirstResponder];
    [tf becomeFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
